﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Dev.Control;
using Bill.BasDLL.biz;
using TT.Common;
using Bill.BasDLL.ety;

namespace Bill.BasForm.sfcorpBill
{
    public partial class frmBillCode : BaseFormDev
    {
        bizBillCode bzBillCode;
        TT.Module.sysLogin.etyBaeSystemParamter basSysPara;
        TT.Module.GetSqliBaseData sqliBaseData;
        public frmBillCode(TT.Module.sysLogin.etyBaeSystemParamter basSysPara)
        {
            this.basSysPara = basSysPara;
            this.sqliBaseData = new TT.Module.GetSqliBaseData(basSysPara.SqliBaseHelp);
            InitializeComponent();
        }
        private string formSta;
        public string FormSta
        {
            set
            {
                formSta = value;
                switch (formSta)
                {
                    case "A":
                        lblBar.Caption = "新建状态";
                        txtItem.Enabled = true;
                        break;
                    case "E":
                        lblBar.Caption = "编辑状态";
                        txtItem.Enabled = false;
                        break;
                    default:
                        break;
                }
            }
            get
            {
                return formSta;
            }
        }
        #region 事件

        //显示行号
        private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle >= 0)
                {
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
                }
                else if (e.RowHandle < 0 && e.RowHandle > -1000)
                {
                    e.Info.Appearance.BackColor = System.Drawing.Color.AntiqueWhite;
                    e.Info.DisplayText = "G" + e.RowHandle.ToString();
                }
            }
        }
        private void frmBillCode_Load(object sender, EventArgs e)
        {
            bzBillCode = new bizBillCode(basSysPara);
            #region 下拉数据
            cboIo_Flag.DataSource = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(sqliBaseData.GetBaseData(basSysPara.PORT_CODE, "CBO_C_BILL_IO"));
            cboItem_Unit.DataSource = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(sqliBaseData.GetBaseData(basSysPara.PORT_CODE, "CBO_C_BILL_UNIT"));

            IList<TT.ORM.etyCodeRef> ilCashFlag = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(sqliBaseData.GetBaseData(basSysPara.PORT_CODE, "CBO_CASH_FLAG"));
            cboCASH_FLAG.DataSource = ilCashFlag;

            #endregion

            #region 表格数据
            ggcBillCode.GridView = this.gridView1;
            ggcBillCode.GridView.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.Click;
            string sMapingCols = "ITEM,ITEM_DESC,ITEM_UNIT,IO_FLAG,CASH_FLAG";
            string sMapingHeaderTxt = "收费代码,代码描述,计费单位,收支标记,现金收费项目";
            ggcBillCode.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt);
            this.winUtil.LoadGridLayout(ggcBillCode);

            RefreshDate();
            #endregion

            FormSta = "A";
            bar3.Text = "状态栏";

        }
        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RefreshDate();
        }

        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            NewData();
        }

        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveDate();
        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DeleteDate();
        }

        private void btnSearch_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.gridView1.OptionsFind.AlwaysVisible = !this.gridView1.OptionsFind.AlwaysVisible;
        }

        private void btnExit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            F12();
        }
        private void ggcBillCode_Click(object sender, EventArgs e)
        {
            etyBillCode ey = srcBinding.Current as etyBillCode;
            if (ey == null)
                return;
            TT.Common.formProcess.SetFormEntity(ey, cGroupControl1);
            cboItem_Unit.GetCodeText();
            cboIo_Flag.GetCodeText();
            cboCASH_FLAG.GetCodeText();
            FormSta = "E";
        }
        #endregion

        public override Dev.Control.etyQuickKey RefreshDate()
        {
            srcBinding.DataSource = bzBillCode.GetBillCode();
            ggcBillCode.DataSource = srcBinding;
            ggcBillCode.GridView.BestFitColumns();
            return base.RefreshDate();
        }

        public override etyQuickKey NewData()
        {
            FormSta = "A";
            ClearForm();
            return base.NewData();
        }

        public override etyQuickKey DeleteDate()
        {
            etyBillCode ey = srcBinding.Current as etyBillCode;
            if (ey == null)
                return null;
            if (TT.HelpInfo.MsgBox.ShowQuestionBox(string.Format("是否删除收费代码：{0}？", new string[] { ey.ITEM })))
            {
                if (bzBillCode.Delete(ey))
                {
                    TT.HelpInfo.MsgBox.ShowSuccessBox("删除成功");
                    lblBar.Caption = "";
                    RefreshDate();
                }
                else
                    TT.HelpInfo.MsgBox.ShowSuccessBox("删除失败");
            }
            return base.DeleteDate();
        }

        public override etyQuickKey SaveDate()
        {
            etyBillCode ey = new etyBillCode();
            TT.Common.formProcess.GetFormEntity(ey, cGroupControl1);
            if (check_data(ey))
            {
                bool rst;
                if (FormSta == "A")
                    rst = bzBillCode.Insert(ey);
                else
                    rst = bzBillCode.Update(ey);
                if (rst)
                {
                    TT.HelpInfo.MsgBox.ShowSuccessBox("保存成功");
                    RefreshDate();
                }
                else
                {
                    TT.HelpInfo.MsgBox.ShowWarnBox("保存失败");
                }
            }
            return base.SaveDate();
        }

        #region 函数
        private void ClearForm()
        {
            txtItem.Text = "";
            txtItem_Desc.Text = "";
            cboItem_Unit.SelectedIndex = -1;
            cboIo_Flag.SelectedIndex = -1;
        }
        private bool check_data(etyBillCode ey)
        {
            if (string.IsNullOrEmpty(ey.ITEM.Trim()))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("收费代码不能为空");
                txtItem.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(ey.ITEM_UNIT.Trim()))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("计费单位不能为空");
                cboItem_Unit.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(ey.ITEM_DESC.Trim()))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("代码描述不能为空");
                txtItem_Desc.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(ey.IO_FLAG.Trim()))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("收支标记不能为空");
                cboIo_Flag.Focus();
                return false;
            }
            return true;
        }
        #endregion

    }
}
